﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsForms
{
    public partial class Add : Form
    {
        public Add()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 更新的构造函数
        /// </summary>
        /// <param name="position"></param>
        /// <param name="name"></param>
        /// <param name="age"></param>
        public Add(string position, string name, int age,int id)
        { 
            InitializeComponent();
            this.id = id;
            textBox1.Text = position;
            textBox2.Text = name;
            textBox3.Text = age.ToString();

        }
        private int id;

        #region 取消
        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
            this.Close();
        }
        #endregion

        #region 确定保存
        private void button1_Click(object sender, EventArgs e)
        {
            if (id > 0)
            {
                if (textBox1.Text.Length == 0 || textBox2.Text.Length == 0 || textBox3.Text.Length == 0)
                {
                    MessageBox.Show("信息不能为空！", "提示");
                }
                else
                {
                    string name = @"^[\u4e00-\u9fa5.·]{0,}$";
                    Regex regex3 = new Regex(name);
                    string sql = string.Format("update users set position='{0}',name='{1}',age='{2}' where id='{3}'", textBox1.Text, textBox2.Text, textBox3.Text,id);
                    int x = int.Parse(textBox3.Text);
                    if (regex3.IsMatch(textBox2.Text) && x > 0 && x < 100)
                    {
                        var get = DBHelp.ChangeSave(sql);
                        if (get >= 0)
                        {
                            MessageBox.Show("更新成功", "提示");
                            this.DialogResult = DialogResult.Yes;
                            this.Close();
                        }
                    }
                    else if (x < 0 || x > 100)
                    {
                        MessageBox.Show("年龄输入错误", "提示");
                    }
                    else
                    {
                        MessageBox.Show("姓名不符合规范", "提示");
                    }
                }
            }
            else if (textBox1.Text.Length == 0 || textBox2.Text.Length == 0 || textBox3.Text.Length == 0)
            {
                MessageBox.Show("信息不能为空！", "提示");
            }
            else
            {
                string name = @"^[\u4e00-\u9fa5.·]{0,}$";
                Regex regex3 = new Regex(name);
                string sql = string.Format("insert into users(position,name,age) values ('{0}','{1}','{2}')", textBox1.Text, textBox2.Text, textBox3.Text);
                int x = int.Parse(textBox3.Text);
                if (regex3.IsMatch(textBox2.Text) && x > 0 && x < 100)
                {
                    var get = DBHelp.ChangeSave(sql);
                    if (get >= 0)
                    {
                        MessageBox.Show("添加成功", "提示");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                }
                else if (x < 0 || x > 100)
                {
                    MessageBox.Show("年龄输入错误", "提示");
                }
                else
                {
                    MessageBox.Show("姓名不符合规范", "提示");
                }
            }
        }
        #endregion
    }

}
